package com.easycarebn.service.location;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import com.timbacsi.main.R;

public class LocationServiceManager implements  LocationListener {
    private static LocationServiceManager mInstance;

    private Context mContext;
    private OnRequestCurrentLocation mCallback;
    
 // flag for GPS status
    private boolean mIsGPSEnabled = false;
 
    // flag for network status
    private boolean mIsNetworkEnabled = false;
 
    private boolean mCanGetLocation = false;
 
    private Location mLocation; // location
    
    protected LocationManager mLocationManager;

    private LocationServiceManager(Context context) {
        mContext = context;
    }

    public static LocationServiceManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new LocationServiceManager(context);
        }
        return mInstance;
    }

    public static interface OnRequestCurrentLocation {
        public void onSuccess(Location location);

        public void onFail();
    }
    
    public Location getLocation() {
        try {
            mLocationManager = (LocationManager) mContext
                    .getSystemService(Context.LOCATION_SERVICE);
 
            // getting GPS status
            mIsGPSEnabled = mLocationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
 
            // getting network status
            mIsNetworkEnabled = mLocationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 
            if (!mIsGPSEnabled && !mIsNetworkEnabled) {
                // no network provider is enabled
                this.mCanGetLocation = false;
            } else {
                this.mCanGetLocation = true;
                // First get location from Network Provider
                if (mIsNetworkEnabled) {
                    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                    Log.d("AAAA", "Network");
                    if (mLocationManager != null) {
                        mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (mIsGPSEnabled) {
                    if (mLocation == null) {
                        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                        Log.d("AAAA", "GPS Enabled");
                        if (mLocationManager != null) {
                            mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        }
                    }
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return mLocation;
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("AAAA", "Location: " + location);
        mLocation = location;
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        
    }
    
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
      
        // Setting Dialog Title
        alertDialog.setTitle(R.string.vi_tri);
  
        // Setting Dialog Message
        alertDialog.setMessage(R.string.ban_co_muon_bat_dinh_vi_vi_tri_khong);
  
        // On pressing Settings button
        alertDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });
  
        // on pressing cancel button
        alertDialog.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });
  
        // Showing Alert Message
        alertDialog.show();
    }
    
    public boolean canGetLocation() {
        return mCanGetLocation;
    }
    
}
